const api = require('../../../../wxapi/config.js')
Page({
	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage: function () {
		return {
			title: '影视行业大数据',
			path: '/pages/index/RankingList/policy/policy'
		}
	},
	/**
	 * 页面的初始数据
	 */
	data: {
    policyList: [],
    paraUp: {
      id: '',
      rq0: ''
    },
    paraDown: {
      id: '',
      rq0: ''
    },
    pageSizeUp: -10,
    pageSizeDown: 10,
    isHasDataUp: true,
    isHasDataDown: true,
		istableloading: false,
		skeleton: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
	},

  getPolicyList(pid, date, size) {
    let textMap = { 10: "已是最新数据", '-10': "没有更多数据了" }
    api.getPolicyList({ newsId: pid, rq: date, pageSize: size }).then((res) => {
      if (0 === res.data.length) {
        wx.showToast({
          title: textMap[size],
          icon: 'none',
          duration: 2500
        })
      } else {
        let dataArr = res.data
        let len = dataArr.length
        if ('' === pid && '' === date) {
          this.setData({
            policyList: dataArr,
            paraUp: dataArr[len - 1],
            paraDown: dataArr[0],
          })
        } else {
          if (10 === size) { // 向下滑 加载时间靠后（最新）数据
            let tempArr = dataArr.reverse()
            for (let i in tempArr) {
              this.data.policyList.unshift(tempArr[i])
            }
            this.setData({
              policyList: this.data.policyList,
            })
            this.setData({
              paraDown: this.data.policyList[0]
            })
          }
          if (-10 === size) { // 向上滑 加载时间靠前数据
            for (let i in dataArr) {
              this.data.policyList.push(dataArr[i])
            }
            this.setData({
              policyList: this.data.policyList,
              paraUp: dataArr[len - 1] // 这样可避免数据反复
            })
          }
        }
      }
    })
  },

  toDetailPage: function (e) {
    wx.navigateTo({
      url: "/pages/index/RankingList/policy/detail/detail?id=" + e.currentTarget.dataset.id
    })
  },

  /**
	 * 生命周期函数--监听页面加载
	 */
  onLoad: function (options) {
		this.setData({
			istableloading: true
		})
		this.getPolicyList(this.data.paraDown.id, this.data.paraDown.rq0, this.data.pageSizeDown)
		setTimeout(() => {
			this.setData({
				istableloading: false
			})
		}, 0)
  },

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
  onPullDownRefresh: function () {
    this.getPolicyList(this.data.paraDown.id, this.data.paraDown.rq0, this.data.pageSizeDown)
    wx.stopPullDownRefresh()
  },

	/**
	 * 页面上拉触底事件的处理函数
	 */
  onReachBottom: function () {
    this.getPolicyList(this.data.paraUp.id, this.data.paraUp.rq0, this.data.pageSizeUp)
  }
})